VDGPORT	.equ	$BFFF
SND		.equ	128
CSS		.equ	64
AG		.equ	32
GM0		.equ	16
GM1		.equ	8
GM2		.equ	4


TCSR	.equ	$0008
ICF		.equ	$80
OCF		.equ	$40
TOF		.equ	$20
EICI	.equ	$10
EOCI	.equ	$08
ETOI	.equ	$04
EIDG	.equ	$02
OLVL	.equ	$01

IRQ2OTI	.equ	$4203
IRQ2OCI	.equ	$4206

CNTREG	.equ	$0009
OCIREG	.equ	$000b	

DEFSTP  .equ	2489

		.org $5000

start
		sei
		ldaa	#$7e
		staa	IRQ2OCI
		ldd		#irq2oci
		std		IRQ2OCI+1
		ldaa	TCSR
		anda	#~(ETOI+EOCI+EICI)
		oraa	#(EOCI)
		staa	TCSR
		cli	
loop

		ldaa	#1
		staa	step+1

		ldaa	#%11111110
		staa	2
		ldaa	3
		bita	#%00000010
		bne		noCtrl
		ldaa	#2
		staa	step+1
noCtrl
		ldaa	#%11111110
		staa	2

;		ldaa	49151
		ldaa	3

		bita	#%00000100
		beq		keyP
		bita	#%00010000
		beq		keyO	
		bita	#%00000010
		beq		exit	
		bra		loop	
keyP
		ldaa	framcnt
waitP	cmpa	framcnt
		beq		waitP

		sei
		ldd		OCIREG
		addd	step
		std		OCIREG		
		cli
		bra		loop
keyO	
		ldaa	framcnt
waitO	cmpa	framcnt
		beq		waitO
		inc	$4000		
		sei
		ldd		OCIREG
		subd	step
		std		OCIREG
		cli
		bra		loop

exit
		rts


irq2oci
		sei
		dec		synccnt
		bne		notSync
		ldaa	#1
		staa	synccnt
		inc		framcnt
notSync
		dec		scancnt
		bne		irq2skip
		ldaa	mode
		eora	#AG+GM2+CSS
		staa	mode
		staa	VDGPORT
		ldaa	#1
		staa	scancnt
irq2skip
		ldaa	TCSR
		ldd		OCIREG
		addd	#DEFSTP
		std		OCIREG
		cli
		rti

mode	.byte	GM0+GM1
step	.word	0
scancnt	.byte	1
synccnt	.byte	3
framcnt	.byte	0
tmpport	.byte	0

		.end

